package easydevelop.popup.actions;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.TreeMap;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import easydevelop.EasydevelopPlugin;

public class GetSrcFilePathAction implements IObjectActionDelegate {

	protected Clipboard sysc = EasydevelopPlugin.sysclip;

	protected static String GETSRCFILEPATH_ACTION_ID = null;

	protected static String LINE_SEP = "\n";

	protected Object[] currentResources = null;

	protected TreeMap prjPathMap = new TreeMap();

	public GetSrcFilePathAction() {
		GETSRCFILEPATH_ACTION_ID = EasydevelopPlugin.getResourceString("getsrcfilepath.action.id");
	}

	public void setActivePart(IAction action, IWorkbenchPart targetPart) {

	}

	public void run(IAction action) {
		if (!GETSRCFILEPATH_ACTION_ID.equals(action.getId())) {
			return;
		}
		if (currentResources == null || currentResources.length == 0) {
			return;
		}
		StringBuffer buf = new StringBuffer();

		for (int i = 0; i < currentResources.length; i++) {
			if (currentResources[i] instanceof IFile) {
				IFile tmpFile = (IFile) currentResources[i];
				String name = tmpFile.getName();
				buf.append(name);
				buf.append(LINE_SEP);
			}
		}
		if (buf.length() > 0) {
			buf.deleteCharAt(buf.length() - 1);
		}
		sysc.setContents(new StringSelection(buf.toString()), null);
	}

	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof StructuredSelection) {
			StructuredSelection ss = (StructuredSelection) selection;
			this.currentResources = ss.toArray();
		}
	}

}
